from datetime import datetime
from typing import Optional, List
from pydantic import BaseModel, Field, constr

# Knowledge Base Schemas
class KnowledgeBaseBase(BaseModel):
    subject: str = Field(..., min_length=1, max_length=50)
    level: Optional[str] = Field(None, pattern='^(初级|中级|高级)$')
    chapter: Optional[str] = Field("第一章", min_length=1, max_length=100)
    knowledge_point: Optional[str] = None
    content: str = Field(..., min_length=1)
    extended_content: Optional[str] = None
    study_count: Optional[int] = Field(0, ge=0)

    # 课程相关字段
    is_course: Optional[bool] = Field(False, description="是否作为课程")
    title: Optional[str] = Field(None, min_length=1, max_length=100)
    description: Optional[str] = None
    credits: Optional[int] = Field(0, ge=0)
    duration: Optional[int] = Field(None, ge=0)
    prerequisites: Optional[str] = None

class KnowledgeBaseCreate(KnowledgeBaseBase):
    pass

class KnowledgeBaseUpdate(KnowledgeBaseBase):
    subject: Optional[str] = Field(None, min_length=1, max_length=50)
    content: Optional[str] = Field(None, min_length=1)

class KnowledgeBaseInDBBase(KnowledgeBaseBase):
    id: int
    created_at: datetime
    updated_at: datetime

    class Config:
        from_attributes = True

class KnowledgeBase(KnowledgeBaseInDBBase):
    exercises: Optional[List["Exercise"]] = []

# Exercise Schemas
class ExerciseBase(BaseModel):
    question: str = Field(..., min_length=1)
    answer: str = Field(..., min_length=1)
    attempt_count: Optional[int] = Field(0, ge=0)
    correct_count: Optional[int] = Field(0, ge=0)

class ExerciseCreate(ExerciseBase):
    knowledge_id: int

class ExerciseUpdate(ExerciseBase):
    question: Optional[str] = Field(None, min_length=1)
    answer: Optional[str] = Field(None, min_length=1)

class ExerciseInDBBase(ExerciseBase):
    id: int
    knowledge_id: int
    created_at: datetime
    updated_at: datetime

    class Config:
        from_attributes = True

class Exercise(ExerciseInDBBase):
    pass

# Update forward references
KnowledgeBase.model_rebuild()

# Learning Plan Schemas
class LearningPlanBase(BaseModel):
    title: str = Field(..., min_length=1, max_length=100)
    description: Optional[str] = None
    subject: Optional[str] = None
    knowledge_base_id: Optional[int] = None  # 关联的知识点/课程ID
    grade_level: Optional[str] = None
    start_date: Optional[datetime] = None
    end_date: Optional[datetime] = None
    status: Optional[str] = "active"
    progress: Optional[int] = Field(0, ge=0, le=100)  # 学习计划的整体进度（百分比）

class LearningPlanCreate(LearningPlanBase):
    pass

class LearningPlanUpdate(LearningPlanBase):
    title: Optional[str] = Field(None, min_length=1, max_length=100)

class LearningPlanInDBBase(LearningPlanBase):
    id: int
    user_id: int
    created_at: datetime
    updated_at: datetime

    class Config:
        from_attributes = True

class LearningPlan(LearningPlanInDBBase):
    tasks: List["LearningTask"] = []
    knowledge_base: Optional[KnowledgeBase] = None  # 关联的知识点/课程信息

# Learning Task Schemas
class LearningTaskBase(BaseModel):
    title: str = Field(..., min_length=1, max_length=100)
    description: Optional[str] = None
    priority: Optional[int] = Field(1, ge=1, le=3)
    duration: Optional[int] = Field(None, ge=0)  # 分钟
    due_date: Optional[datetime] = None
    status: Optional[str] = "pending"
    completion_rate: Optional[float] = Field(0.0, ge=0.0, le=100.0)

class LearningTaskCreate(LearningTaskBase):
    plan_id: int

class LearningTaskUpdate(LearningTaskBase):
    title: Optional[str] = Field(None, min_length=1, max_length=100)

class LearningTaskInDBBase(LearningTaskBase):
    id: int
    plan_id: int
    created_at: datetime
    updated_at: datetime

    class Config:
        from_attributes = True

class LearningTask(LearningTaskInDBBase):
    progress_records: List["Progress"] = []

# Progress Schemas
class ProgressBase(BaseModel):
    status: str
    notes: Optional[str] = None
    time_spent: Optional[int] = Field(None, ge=0)  # 分钟
    rating: Optional[int] = Field(None, ge=1, le=5)

class ProgressCreate(ProgressBase):
    task_id: int

class ProgressUpdate(ProgressBase):
    pass

class ProgressInDBBase(ProgressBase):
    id: int
    task_id: int
    user_id: int
    created_at: datetime

    class Config:
        from_attributes = True

class Progress(ProgressInDBBase):
    pass

# Update forward references
LearningPlan.model_rebuild()
LearningTask.model_rebuild()